
#include "sfsis.h"
#include "sfcmd.h"
#include "debug.h"

/*sfcmd中处理了来自无线协议栈的信息，并整合进整机的寄存器操作中*/
SF_Boolen sf_cmd_page_write(SF_Page page, unsigned char startAddr, unsigned char *buf, unsigned char count)
{
    SF_Boolen ret = SF_False;
    su16 ii;
    su16 iMax;
    switch (page)
    {
    case PAGE0_REGS:
        //DPRINT(DEBUG_INFO, "[SFCMD] Write PAGE0_REGS.\r\n");
        ret = config_setMultRegs(startAddr, buf, count);
        break;
    case PAGE1_RESERVED:
        //DPRINT(DEBUG_INFO, "[SFCMD] Write PAGE1_RESERVED.\r\n");
        break;
    case PAGE2_RESERVED:
        //DPRINT(DEBUG_INFO, "[SFCMD] Write PAGE2_RESERVED.\r\n");
        break;
    case PAGE3_RESERVED:
        //DPRINT(DEBUG_INFO, "[SFCMD] Write PAGE3_RESERVED.\r\n");
        break;
    case PAGE4_MEMORY_MAP1:
        //DPRINT(DEBUG_INFO, "[SFCMD] Write PAGE4_MEMORY_MAP1.\r\n");
        if (gCfgCommonPtr->directAccess != 0xAA)
        {
            break;
        }
        iMax = startAddr + count;
        if (iMax > 255)
        {
            iMax = 255;
        }

        for (ii = startAddr; ii < iMax; ii++)
        {
            rgbStrip.memory[ii] = *buf;
            buf++;
        }

        break;
    case PAGE5_MEMORY_MAP2:
        //DPRINT(DEBUG_INFO, "[SFCMD] Write PAGE5_MEMORY_MAP2.\r\n");
        if (gCfgCommonPtr->directAccess != 0xAA)
        {
            break;
        }
        iMax = startAddr + count;

        if (iMax > 512)
        {
            iMax = 512;
        }

        for (ii = startAddr; ii < iMax; ii++)
        {
            rgbStrip.memory[ii] = *buf;
            buf++;
        }

        break;
    case PAGE6_MEMORY_MAP3:
        //DPRINT(DEBUG_INFO, "[SFCMD] Write PAGE6_MEMORY_MAP3.\r\n");
        if (gCfgCommonPtr->directAccess != 0xAA)
        {
            break;
        }
        iMax = startAddr + count;

        if (iMax > 768)
        {
            iMax = 768;
        }

        for (ii = startAddr; ii < iMax; ii++)
        {
            rgbStrip.memory[ii] = *buf;
            buf++;
        }

        break;
    case PAGE_MAX:
    default:
        break;
    }
    return ret;
}

su8 sf_cmd_page_read(SF_Page page, unsigned char startAddr, unsigned char *buf, unsigned char readLen)
{
    su16 ii;
    su16 iMax;
    su8 ret = 0;
    if (readLen > 63)
    {
        readLen = 63;
    }

    switch (page)
    {
    case PAGE0_REGS:
        //DPRINT(DEBUG_INFO, "[SFCMD] Read PAGE0_REGS.\r\n");
        ret = config_getMultRegs(startAddr, buf, readLen);
        break;
    case PAGE1_RESERVED:
        //DPRINT(DEBUG_INFO, "[SFCMD] Read PAGE1_RESERVED.\r\n");
        break;
    case PAGE2_RESERVED:
        //DPRINT(DEBUG_INFO, "[SFCMD] Read PAGE2_RESERVED.\r\n");
        break;
    case PAGE3_RESERVED:
        //DPRINT(DEBUG_INFO, "[SFCMD] Read PAGE3_RESERVED.\r\n");
        break;
    case PAGE4_MEMORY_MAP1:
        //DPRINT(DEBUG_INFO, "[SFCMD] Read PAGE4_MEMORY_MAP1.\r\n");
        iMax = startAddr + readLen;
        if (iMax > 256)
        {
            iMax = 256;
        }

        for (ii = startAddr; ii < iMax; ii++)
        {
            *buf = rgbStrip.memory[ii];
            buf++;
            ret++;
        }

        break;
    case PAGE5_MEMORY_MAP2:
        //DPRINT(DEBUG_INFO, "[SFCMD] Read PAGE5_MEMORY_MAP2.\r\n");
        iMax = startAddr + readLen + 256;
        if (iMax > 512)
        {
            iMax = 512;
        }

        for (ii = startAddr; ii < iMax; ii++)
        {
            *buf = rgbStrip.memory[ii];
            buf++;
            ret++;
        }

        break;
    case PAGE6_MEMORY_MAP3:
        //DPRINT(DEBUG_INFO, "[SFCMD] Read PAGE6_MEMORY_MAP3.\r\n");
        iMax = startAddr + readLen + 512;
        if (iMax > 768)
        {
            iMax = 768;
        }

        for (ii = startAddr; ii < iMax; ii++)
        {
            *buf = rgbStrip.memory[ii];
            buf++;
            ret++;
        }

        break;
    case PAGE_MAX:
    default:
        break;
    }
    return ret;
}

SF_Boolen sf_cmd_task(void)
{
    SF_Boolen ret = SF_False;
    su8 *rBuf;
    su8 *tBuf;
    su8 rLen;
    //接收缓冲区非空
    if (bsf_isRecvBufNE())
    {

        rBuf = bsf_recvBegin(&rLen);
        //DPRINT(DEBUG_INFO, "[SF MAIN]: Recv buf not empty.rlen:%d [0]:%x, [1]:%x, [2]:%x ,[3]:%x\r\n", rLen, rBuf[0], rBuf[1], rBuf[2], rBuf[3]);
        if (rLen < 3)
        {
            //DPRINT(DEBUG_WARN, "[SF MAIN]: data pack not completed.\r\n");
            bsf_recvEnd(SF_True);
            return SF_False; //结束处理
        }
        //取出高两位，判断当前包属于什么类型
        //00 主机写
        //01 主机读
        //10 从机读返回
        //11 从机报告
        //buf组成结构
        //          0              1         2           3
        //[rw[7:6] page[5-0]]   [count] [startaddr]  [data....]
        switch (rBuf[0] & SF_CMD_RW_MASK)
        {
        case SF_CMD_MASTER_WRITE:
            if (rBuf[1] + 3 == rLen)
            {
                sf_cmd_page_write(rBuf[0] & SF_CMD_PAGE_MASK, rBuf[2], &rBuf[3], rBuf[1]);
                ret = SF_True;
            }
            bsf_recvEnd(SF_True);
            break;
        case SF_CMD_MASTER_READ:
            tBuf = bsf_sendBegin();
            if (tBuf != 0 && rBuf[1] != 0)
            {
                rLen = sf_cmd_page_read(rBuf[0] & SF_CMD_PAGE_MASK, rBuf[2], &tBuf[3], rBuf[1]);
                tBuf[0] = (rBuf[0]& SF_CMD_PAGE_MASK) | SF_CMD_SLAVE_RET;
                tBuf[1] = rLen;
                tBuf[2] = rBuf[2];
                ret = SF_True;
            }
            bsf_recvEnd(SF_True);
            bsf_sendEnd(rLen + 3);
            break;
        case SF_CMD_SLAVE_RET:
            bsf_recvEnd(SF_True);
            ret = SF_False;
            //do nothing
            break;
        case SF_CMD_SLAVE_REPORT:
            bsf_recvEnd(SF_True);
            ret = SF_False;
            //do nothing
            break;
        default:
            bsf_recvEnd(SF_True);
            ret = SF_False;
            break;
        }
    }
    return ret;
}